﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ProjManagSolution.pmMyWebService;
//remember to add phase list
namespace ProjManagSolution
{
    public partial class CreateTask : ChildWindow
    {
        // List of auto collection
        protected List<Phase> listPhase = new List<Phase>();
        protected List<MemberEmail> listEmail = new List<MemberEmail>();

        protected Phase selectedPhase = new Phase();
        protected MemberEmail selectedMember = new MemberEmail();


        public CreateTask(List<phase> pl, List<user> ml)
        {
            InitializeComponent();

            // Add items into collection;
            AddCollectionPhaseMember(pl, ml);
            // Bind datacontext
            autoboxPhase.DataContext = listPhase;
            autoboxAssignedMember.DataContext = listEmail;
        }

        protected bool Input_Validate()
        {
            if (autoboxAssignedMember.SelectedItem == null)
            {
                MessageBox.Show("Please select a member!");
                return false;
            }

            if (autoboxPhase.SelectedItem == null)
            {
                MessageBox.Show("Please select a phase!");
                return false;
            }

            if ((datepickStartDate.SelectedDate < DateTime.Now) ||
                 (datepickDeadLine.SelectedDate < DateTime.Now))
            {
                MessageBox.Show("Start date/Deadline cannot be ealier than today!");
                return false;
            }
            if (DateTime.Compare((DateTime)datepickDeadLine.SelectedDate, (DateTime)datepickStartDate.SelectedDate) < 0)
            {
                MessageBox.Show("Start date must be ealier than deadline!");
                return false;
            }


            if (txtboxTaskName.Text == "")
            {
                MessageBox.Show("Taskname cannot be empty!");
                return false;
            }
            return true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Input_Validate())
                this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        // Add items into collection;
        private void AddCollectionPhaseMember(List<phase> pl, List<user> ml)
        {
            Phase tempP = new Phase();
            for (int i = 0; i < pl.Count; ++i)
            {
                //ADD phase list
                tempP.ID = pl[i].phase_id.ToString();
                tempP.Name = pl[i].phase_name;
                
                listPhase.Add(tempP);
            }

            for (int i = 0; i < ml.Count; ++i)
            {
                listEmail.Add(new MemberEmail(ml[i].user_email, ml[i].user_id.ToString()));
            }
        }
    }
}

